package com.byxy.dao;

import com.byxy.model.PayInfo;
import com.byxy.util.DBUtil;
import com.byxy.util.DateUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PayInfoDaoImpl implements BaseDao<PayInfo> {
	private PreparedStatement psta = null;
	private Connection conn = null;
	private ResultSet rs = null;

	@Override
	public int add(PayInfo pay) {
		int i = -1;
		int id = pay.getId();
		String name = pay.getName();
		Date hours = pay.getHours();
		double cost = pay.getCost();
		String text = pay.getText();
		int basicInfoId = pay.getBasicInfoId();
		String sql = "INSERT INTO PayInfo VALUES(?, ?, ?, ?, ?, ?)";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			psta.setString(2, name);
			psta.setTimestamp(3, DateUtil.dateToTimestamp(hours));
			psta.setDouble(4, cost);
			psta.setString(5, text);
			psta.setInt(6, basicInfoId);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public int deleteById(int id) {
		int i = -1;
		String sql = "DELETE FROM PayInfo WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public int update(PayInfo pay) {
		int i = -1;
		int id = pay.getId();
		String name = pay.getName();
		Date hours = pay.getHours();
		double cost = pay.getCost();
		String text = pay.getText();
		int basicInfoId = pay.getBasicInfoId();
		String sql = "UPDATE PayInfo SET name = ?, hours = ?, cost = ?, text = ? basicInfoId = ? WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setString(1, name);
			psta.setTimestamp(2, DateUtil.dateToTimestamp(hours));
			psta.setDouble(3, cost);
			psta.setString(4, text);
			psta.setInt(5, basicInfoId);
			psta.setInt(6, id);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public List<PayInfo> findAll() {
		List<PayInfo> list = new ArrayList<PayInfo>();
		String sql = "SELECT * FROM PayInfo";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				Timestamp hours = rs.getTimestamp("hours");
				double cost = rs.getDouble("cost");
				String remarks = rs.getString("Remarks");
				int basicInfoId = rs.getInt("basicInfoId");
				list.add(new PayInfo(id, name, DateUtil.timestempToDate(hours), cost, remarks, basicInfoId));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public PayInfo findById(int id) {
		PayInfo pay = null;
		String sql = "SELECT * FROM PayInfo WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			rs = psta.executeQuery();
			while (rs.next()) {
				int uid = rs.getInt("id");
				String name = rs.getString("name");
				Timestamp hours = rs.getTimestamp("hours");
				double cost = rs.getDouble("cost");
				String remarks = rs.getString("Remarks");
				int basicInfoId = rs.getInt("basicInfoId");
				pay = new PayInfo(uid, name, DateUtil.timestempToDate(hours), cost, remarks, basicInfoId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return pay;
	}

}
